<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CouponActivity;
use App\Admin\show\Coupon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CouponActivityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponActivity(), function (Grid $grid) {
            $grid->column('activity_id')->sortable();
            $grid->column('activity_name');
            $grid->column('activity_content');
            $grid->column('activity_status','是否开启')->switch('',true);
            $grid->column('coupon_id','指定优惠券')->display('指定优惠券')->modal(function ($model){
                $model->title('优惠券');
                return Coupon::make(['coupon_id'=>$this->coupon_id]);
            });
            $grid->column('activity_stock')->display(function ($e) {
                return $e . '张';
            })->label();
            $grid->column('receive_restrict_number')->display(function ($e) {
                return $e . '次';
            })->label();
            $grid->column('activity_start_time');
            $grid->column('activity_exit_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CouponActivity(), function (Show $show) {
            $show->field('activity_id');
            $show->field('activity_name');
            $show->field('activity_content');
            $show->field('activity_status');
            $show->field('coupon_id');
            $show->field('activity_stock');
            $show->field('receive_restrict_number');
            $show->field('activity_start_time');
            $show->field('activity_exit_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponActivity(), function (Form $form) {
            $form->text('activity_id','ID');
            $form->text('activity_name')->required();
            $form->text('activity_content')->required();
//            $form->radio('activity_status')->required()->options(['开启', '关闭']);
            $form->switch('activity_status')
                ->customFormat(function ($v) {
                    return $v == '1' ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v ? '1' : '0';
                });

            $form->selectTable('coupon_id', '请选择活动抢购的优惠券')//类型为指定商品优惠券就选择商品
            ->from(Coupons::make())->saving(function ($e) {
                return $e;
            });
            $form->text('activity_stock')->required()->type('number')->maxLength(4);
            $form->text('receive_restrict_number','可参与次数')->required();
            $form->datetime('activity_start_time')->required();
            $form->datetime('activity_exit_time')->required();

            $form->display('created_at');
            $form->display('updated_at');

//            $form->submitted(function ($e){
//                $couponData=\App\Models\Coupon::where('coupon_id',$e->coupon_id)->pluck('coupon_stock')->toArray();
//                if($e->activity_stock>$couponData[0]){
//                    return $e->responseValidationMessages('content', '发放数量不能大于优惠券库存数量!');
//                }
//            });
        });
    }
}
